#include "app_dev.h"

app_alarm_mgr_t sg_alarm_mgr = {0};
app_burn_mgr_t sg_burn_mgr = {0};

void app_wakeup_alarm_led_task(void)
{
    if (app_get_menu_state() == MENU_STANDBY || app_get_menu_state() == MENU_BAT_LOCK_STANDBY || app_get_menu_state() == MENU_TIP)
    {
        return;
    }
    else
    {
        if (sg_alarm_mgr.jam == 1)
        {
            app_led_toggle(APP_LED_JAM, APP_TOGGLE_SLOW_TIME, 0x00);
        }
        if (sg_alarm_mgr.bat_low == 1)
        {
            if ((dp_obj->fault & (1 << BAT_LOW)) == 0) // 故障发生上报
            {
                dp_obj->fault |= (1 << BAT_LOW);
                app_dp_fault_notify();
            }
            app_led_toggle(APP_LED_BAT, APP_TOGGLE_SLOW_TIME, 0x00);
        }
        else 
        {
            if (dp_obj->fault & (1 << BAT_LOW)) // 故障清除上报
            {
                dp_obj->fault &= ~(1 << BAT_LOW);
                app_dp_fault_notify();
            }           
            app_led_off(APP_LED_BAT);
        }
    }
}

void app_set_alarm_jam(unsigned char value)
{
    sg_alarm_mgr.jam = value;
}

void app_set_alarm_bat_low(unsigned char value)
{
    sg_alarm_mgr.bat_low = value;
}

static void app_dev_wifi_state_cb_handler(const unsigned char *data, unsigned short length)
{
    if (length != 0x01)
    {
        return;
    }

    unsigned char state = data[0];
    switch (state)
    {
    case WIFI_FAST_CONF:
    {
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
        break;
    }
    case WIFI_AP_CONF:
    {
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
        break;
    }
    case WIFI_STA_DISCONN:
    {
        dev_obj->tuya_net = TUYA_NET_NONE;
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_SLOW_TIME, 0x00);
        break;
    }
    case WIFI_STA_CONN:
    {
        dev_obj->tuya_net = TUYA_NET_NONE;
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_SLOW_TIME, 0x00);
        break;
    }
    case WIFI_NET_OK:
    {
        dev_obj->tuya_net = TUYA_NET_OK;
        app_led_on(APP_LED_WIFI);
        break;
    }
    case WIFI_LOW_POWER:
    {
        app_led_off(APP_LED_WIFI);
        break;
    }
    case WIFI_AP_BLE_CONF:
    {
        dev_obj->tuya_net = TUYA_NET_NONE;
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
        break;
    }
    default:
        break;
    }
}

static void app_dev_wifi_reset_cb_handler(const unsigned char *data, unsigned short length)
{
    if (length != 0x02 || data[0] != 0x05)
    {
        return;
    }

    switch (data[1])
    {
    case 0x00:
    {
        // local reset
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
        if (app_release_is_finished())
        {
            dp_obj->state = FEED_STANDBY;
        }
        break;
    }

    case 0x01:
    {
        // app remote reset
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
        if (app_release_is_finished())
        {
            dp_obj->state = FEED_STANDBY;
        }
        break;
    }

    case 0x02:
    {
        // app reset facoty
        app_plan_clear_to_flash();
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
        if (app_release_is_finished())
        {
            dp_obj->state = FEED_STANDBY;
        }
        break;
    }

    default:
        break;
    }
}

static void app_dev_wifi_btn_reset_cb_handler(app_btn_t btn, app_btn_state_t state)
{
    // bat 供电无法进行配网
    if (dev_obj->type == BAT_TYPE)
    {
        return;
    }

    if (state == APP_LONG_CLICK)
    {
        app_uart_tx_wifi_smart_config_reset();
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
    }
}

static void app_dev_wifi_btn_shutdown_cb_handler(app_btn_t btn, app_btn_state_t state)
{
    // dc 供电无关机功能
    if (dev_obj->type == DC_TYPE)
    {
        return;
    }

    if (dev_obj->shutdown == SHUTDOWN_NONE)
    {
        dev_obj->shutdown = SHUTDOWN_OK;
    }
}

static void app_dev_wifi_btn_start_up_cb_handler(app_btn_t btn, app_btn_state_t state)
{
    // dc 供电无开机功能
    if (dev_obj->type == DC_TYPE)
    {
        return;
    }

    if (dev_obj->shutdown == SHUTDOWN_OK)
    {
        dev_obj->shutdown = SHUTDOWN_NONE;
    }
}

static void app_dev_menu_btn_start_feed_cb_handler(app_btn_t btn, app_btn_state_t state)
{
    app_release_set_value(0x01);
}

void app_dev_init(void)
{
    app_uart_cb_register(WIFI_STATE_CB, app_dev_wifi_state_cb_handler);
    app_uart_cb_register(WIFI_RESET_CB, app_dev_wifi_reset_cb_handler);
    app_uart_cb_register(LMT_NOTIF_CB, app_time_cb_handler);
    app_uart_cb_register(OTA_NOTIF_CB, app_ota_notif_cb_handler);
    app_uart_cb_register(OTA_TRANS_CB, app_ota_trans_cb_handler);
    app_uart_cb_register(DPID_GET_CB, app_dp_get_cb_handler);
    app_uart_cb_register(DPID_SET_CB, app_dp_set_cb_handler);
    app_uart_cb_register(FCTR_WFI_READY_CB, app_factory_wifi_ready_cb_handler);
    app_uart_cb_register(FCTR_WFI_RSI_CB, app_factory_wifi_rssi_cb_handler);

    app_btn_effective_set(APP_BTN_ENTER, BTN_EFFECTIVE_LOW);
    app_btn_effective_set(APP_BTN_A, BTN_EFFECTIVE_LOW);

    //if (dev_obj->factory.state == FACTORY_NONE)
    {
        // btn a
        app_btn_single_click_cb_register(APP_BTN_A, app_dev_btn_rotate_cb_handler);

        // btn enter
        app_btn_long_click_cb_register(APP_BTN_ENTER, app_dev_enter_btn_resetwifi_cb_handler, 5000);

        app_btn_double_click_cb_register(APP_BTN_ENTER, app_dev_btn_esc_burn_cb_handler, 1000);
        app_btn_multiple_click_cb_register(APP_BTN_ENTER, app_dev_btn_enter_burn_cb_handler, 1000, 5);

        app_btn_single_click_cb_register(APP_BTN_ENTER, app_dev_enter_btn_feed_cb_handler);
        app_menu_Init();
    }
    //else
    {
        // app_btn_factory_cb_register(app_factory_btn_pressed_cb_handler);
    }

    if (dev_obj->type == DC_TYPE)
    {
        app_wifi_set_on();
        app_uart_init();
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
    }
}

static void app_dev_sleep_led_handler(void)
{
    if (dev_obj->type == DC_TYPE)
    {
        return;
    }

    if (dev_obj->sleep.state == SLEEP_NONE)
    {
        // 关串口、关WiFi
        app_uart_init();
        app_wifi_set_off();

        if (dev_obj->shutdown == SHUTDOWN_NONE)
        {
            // 蓝灯亮200ms, 灭2800ms
            app_led_on(APP_LED_WIFI);
            dev_obj->sleep.state = SLEEP_LED_ON;
            dev_obj->sleep.time = app_sys_ticks;
        }
        else
        {
            dev_obj->sleep.state = SLEEP_LED_OFF;
        }
    }

    else if (dev_obj->sleep.state == SLEEP_LED_ON)
    {
        if (time_after(app_sys_ticks, dev_obj->sleep.time + 8))
        {
            app_led_off(APP_LED_WIFI);
            dev_obj->sleep.state = SLEEP_LED_OFF;
            dev_obj->sleep.time = app_sys_ticks;
        }
    }

    else if (dev_obj->sleep.state == SLEEP_LED_OFF)
    {
        if (dev_obj->shutdown == SHUTDOWN_NONE)
        {
            // 未设置关机， 3秒后亮蓝灯
            if (time_after(app_sys_ticks, dev_obj->sleep.time + 3000))
            {
                app_led_on(APP_LED_WIFI);
                dev_obj->sleep.state = SLEEP_LED_ON;
                dev_obj->sleep.time = app_sys_ticks;
            }
        }
    }
}

static void app_dev_wakeup_handler(void)
{
    // DC供电，唤醒
    if (dev_obj->type == DC_TYPE && dev_obj->sleep.state != SLEEP_NONE)
    {
        dev_obj->sleep.state = SLEEP_NONE;
        dev_obj->shutdown = SHUTDOWN_NONE;
        // app_wifi_set_on();
        app_uart_init();
        app_led_toggle(APP_LED_WIFI, APP_TOGGLE_FAST_TIME, 0x00);
    }
}

void app_dev_task(void)
{
    app_dev_sleep_led_handler();
    app_dev_wakeup_handler();
}

unsigned char app_dev_is_ok_to_sleep(void)
{
    unsigned char okay = 0x00;

    if (app_release_is_ok() == 1 && (app_get_menu_state() == MENU_BAT_LOCK_STANDBY || app_get_menu_state() == MENU_TIP || app_get_menu_state() == MENU_STANDBY) && app_get_buzzer_state() == APP_BUZZER_OFF)
    {
        okay = 0x01;
    }

    return okay;
}